package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.AppTenant;
import com.un.ebs.sys.domain.Configure;
import com.un.ebs.sys.domain.ConfigureValue;
import com.un.ebs.sys.dto.ConfigureAddDto;
import com.un.ebs.sys.dto.ConfigureUpdateDto;
import com.un.ebs.sys.mapper.ConfigureMapper;
import com.un.ebs.sys.mapper.ConfigureValueMapper;
import com.un.ebs.sys.service.AppTenantService;
import com.un.ebs.sys.service.ConfigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2019-11-01
 */
@Service
public class ConfigureServiceImpl extends ServiceImpl<ConfigureMapper, Configure> implements ConfigureService {

    @Autowired
    private ConfigureValueMapper configureValueMapper;

    @Autowired
    private AppTenantService appTenantService;

    @Override
    public void add(ConfigureAddDto req) {
        Configure obj = new Configure();
        obj.setCode(req.getCode());
        obj.setName(req.getName());
        obj.setRemark(req.getRemark());
        obj.setScope(req.getScope());
        obj.setValueType(req.getValueType());
        obj.setDefaultValue(req.getDefaultValue());
        obj.setAppId(req.getAppId() == "" ? SecurityUtils.getUserContent().getAppId() : req.getAppId());
        obj.setGroupId(req.getGroupId());
        obj.setIsDelete(false);
        obj.insert();
    }

    @Override
    public void update(ConfigureUpdateDto req) {
        Configure obj = getById(req.getId());
        obj.setCode(req.getCode());
        obj.setName(req.getName());
        obj.setRemark(req.getRemark());
        obj.setScope(req.getScope());
        obj.setValueType(req.getValueType());
        obj.setDefaultValue(req.getDefaultValue());
        obj.setGroupId(req.getGroupId());
        obj.updateById();
    }

    @Override
    public Configure queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public String getValueByCode(String code) {
        return getValueByCode(code, SecurityUtils.getTenantId());
    }

    @Override
    public Double getValueDoubleByCode(String code) {
        String value = getValueByCode(code);
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        } else {
            return Double.valueOf(value);
        }
    }

    @Override
    public String getValueByCode(String code, String tenantId) {
        if (StringUtils.isNullOrEmpty(code)) {
            return null;
        }
        AppTenant tenant = appTenantService.queryById(tenantId);
        if (tenant == null) {
            throw new BusinessEntityNotFoundException(tenantId, "租户");
        }

        LambdaQueryWrapper<Configure> lq = Wrappers.lambdaQuery();
        lq.eq(Configure::getIsDelete, false);
        lq.eq(Configure::getAppId, tenant.getAppId());
        lq.eq(Configure::getCode, code);
        Configure configure = baseMapper.selectOne(lq);
        if (configure == null) {
            return null;
        }
        return this.getValueById(configure.getId(), tenantId);
    }

    @Override
    public String getValueById(String id) {
        return getValueById(id, SecurityUtils.getTenantId());
    }

    @Override
    public String getValueById(String id, String tenantId) {
        Configure configure = baseMapper.selectById(id);
        if (configure == null) {
            throw new BusinessEntityNotFoundException(id, "配置");
        }
        if (StringUtils.isNullOrEmpty(tenantId)) {
            throw new BusinessException("租户ID 不能为空。");
        }

        LambdaQueryWrapper<ConfigureValue> lq = Wrappers.lambdaQuery();
        lq.eq(ConfigureValue::getIsDelete, false);
        lq.eq(ConfigureValue::getConfigureId, id);
        lq.eq(ConfigureValue::getTenantId, tenantId);
        ConfigureValue configureValue = configureValueMapper.selectOne(lq);
        if (configureValue == null) {
            return configure.getDefaultValue();
        } else {
            return configureValue.getValue();
        }
    }

    @Override
    public void delete(String id) {
        getBaseMapper().deleteById(id);
    }

}